﻿/*
=======================================================
    Bill【单据】
=======================================================
*/

namespace Services
{
    public interface IBillMapping : IMapping<Bill, BillModel, BillFilterModel>
    { }

    public class BillMapping : AutoBillMapping, IBillMapping
    {
        public BillMapping(ICurrentUser currentUser)
            : base(currentUser)
        {
        }

        #region 读数据
        /// <summary>
        /// 读数据，从Entity对象转成Model对象
        /// </summary>
        /// <param name="entity">Entity对象</param>
        /// <returns></returns>
        public override BillModel Entity2Model(Bill entity)
        {
            if (entity == null)
            {
                return null;
            }

            BillModel model = base.Entity2Model(entity);


            /***************************************** 下面处理自定义转换 *****************************************/
            model.OperType_Name = entity.OperType_Name;
            model.Warehouse_Name = entity.Warehouse_Name;


            return model;
        }
        #endregion

        #region 写数据
        /// <summary>
        /// 写数据，从Model对象转换成Entity对象
        /// </summary>
        /// <param name="model">ViewModel对象</param>
        /// <returns></returns>
        public override Bill Model2Entity(BillModel model)
        {
            Bill entity = base.Model2Entity(model);


            /***************************************** 下面处理自定义转换 *****************************************/



            return entity;
        }
        #endregion

        #region 读数据(集合)
        /// <summary>
        /// 读数据，从Entity对象转成Model对象
        /// </summary>
        /// <param name="entities">Entity对象</param>
        /// <returns></returns>
        public override List<BillModel> Entity2Model(IEnumerable<Bill> entities)
        {
            return entities.Select(x => Entity2Model(x)).ToList();
        }
        #endregion

        #region 写数据(集合)
        /// <summary>
        /// 写数据，从Model对象转换成Entity对象
        /// </summary>
        /// <param name="models">ViewModel对象</param>
        /// <returns></returns>
        public override List<Bill> Model2Entity(IEnumerable<BillModel> models)
        {
            return models.Select(x => Model2Entity(x)).ToList();
        }
        #endregion

        #region 搜索条件转换
        /// <summary>
        /// 搜索条件转换，从FilterModel对象转换成Where对象
        /// </summary>
        /// <param name="filterModel">FilterModel对象</param>
        /// <returns></returns>
        public override Expressionable<Bill> FilterModel2Where(BillFilterModel filterModel)
        {
            var where = base.FilterModel2Where(filterModel);

            //处理数据权限
            if (_currentUser.CurrentUserInfo.LoginUserType == LoginUserType.集团)
            {
                where = where.And(x => _currentUser.CurrentUserInfo.LoginUserWarehouseIDsWithDataDepartment.Contains(x.WarehouseID));
            }

            //SearchInput
            if (!string.IsNullOrEmpty(filterModel._SearchInput))
            { }

            ////申请人
            //if (filterModel._ApplyUserID.HasValue)
            //{
            //    where = where.And(x => x.ApplyUserID == filterModel._ApplyUserID.Value);
            //}

            ////提交人
            //if (filterModel._SubmitUserID.HasValue)
            //{
            //    where = where.And(x => x.SubmitUserID == filterModel._SubmitUserID.Value);
            //}

            ////退回/作废人
            //if (filterModel._BackUserID.HasValue)
            //{
            //    where = where.And(x => x.BackUserID == filterModel._BackUserID.Value);
            //}

            ////确认人
            //if (filterModel._ConfirmUserID.HasValue)
            //{
            //    where = where.And(x => x.ConfirmUserID == filterModel._ConfirmUserID.Value);
            //}

            ////打回原因
            //if (!string.IsNullOrEmpty(filterModel._BackReason))
            //{
            //    where = where.And(x => x.BackReason.Contains(filterModel._BackReason));
            //    //where = where.And(x => x.BackReason == filterModel._BackReason);
            //}

            ////创建人
            //if (filterModel._CreateUserID.HasValue)
            //{
            //    where = where.And(x => x.CreateUserID == filterModel._CreateUserID.Value);
            //}


            return where;
        }
        #endregion
    }
}
